package com.example.albumphoto

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

// PhotoAdapter.kt
class PhotoAdapter(
    private val photos: List<PreViewDataBean>,
    private val onItemClick: (PreViewDataBean) -> Unit
) : RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder>() {
    private val TAG = "PhotoAdapter"
    inner class PhotoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val imageView: ImageView = itemView.findViewById(R.id.photoImageView)
        private val titleView: TextView = itemView.findViewById(R.id.photoTitleTextView)

        fun bind(photo: PreViewDataBean) {
            Log.i(TAG, "photo.imageResId = ${photo.imageResId}")
            imageView.setImageResource(photo.imageResId)
            titleView.text = photo.title

            itemView.setOnClickListener {
                onItemClick(photo)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_photo, parent, false)
        return PhotoViewHolder(view)
    }

    override fun onBindViewHolder(holder: PhotoViewHolder, position: Int) {
        holder.bind(photos[position])
    }

    override fun getItemCount(): Int = photos.size
}